<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">{{ $t('inventory.batch') }}</label>
        <el-input v-model="query.batch" clearable :placeholder="$t('inventory.batch')" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">{{ $t('inventory.workNum') }}</label>
        <el-input v-model="query.workNum" clearable :placeholder="$t('inventory.workNum')" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">{{ $t('inventory.inventoryStatus') }}</label>
        <el-select v-model="query.inventoryStatus" class="filter-item" clearable filterable :placeholder="$t('common.selectPlaceholder')">
          <el-option
            v-for="item in dict.kucun_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">{{ $t('inventory.storageCode') }}</label>
        <el-input v-model="query.storageCode" clearable :placeholder="$t('inventory.storageCode')" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">{{ $t('inventory.palletCode') }}</label>
        <el-input v-model="query.palletCode" clearable :placeholder="$t('inventory.palletCode')" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="35%">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="auto">
          <el-form-item :label="$t('inventory.batch')" prop="batch">
            <el-input v-model="form.batch" style="width: 370px;" />
          </el-form-item>
          <el-form-item :label="$t('inventory.workNum')" prop="workNum">
            <el-input v-model="form.workNum" style="width: 370px;" />
          </el-form-item>
          <el-form-item :label="$t('inventory.storageCode')" prop="storageCode">
            <el-input v-model="form.storageCode" style="width: 370px;" />
          </el-form-item>
          <el-form-item :label="$t('inventory.palletCode')" prop="palletCode">
            <el-input v-model="form.palletCode" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">{{ $t('crud.cancel') }}</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="submitCU">{{ $t('crud.confirm') }}</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="id" />
        <el-table-column prop="batch" :label="$t('inventory.batch')" />
        <el-table-column prop="workNum" :label="$t('inventory.workNum')" />
        <el-table-column prop="inventoryStatus" :label="$t('inventory.inventoryStatus')">
          <template slot-scope="scope">
            {{ dict.label.kucun_status[scope.row.inventoryStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="inBoundFinishTime" :label="$t('inventory.inBoundFinishTime')" />
        <el-table-column prop="storageCode" :label="$t('inventory.storageCode')" />
        <el-table-column prop="palletCode" :label="$t('inventory.palletCode')" />
        <el-table-column prop="layerName" :label="$t('inventory.layerName')" />
        <el-table-column v-if="checkPer(['admin','inventory:edit','inventory:del'])" :label="$t('common.operation')" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudInventory from '@/api/inventory'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, batch: null, workNum: null, inventoryStatus: null, inBoundFinishTime: null, storageCode: null, palletCode: null, processcardCode: null }
export default {
  name: 'Inventory',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['kucun_status'],
  cruds() {
    return CRUD({ title: '库存', url: 'api/inventory', idField: 'id', sort: 'id,desc', optShow: {
      add: false,
      edit: true,
      del: true,
      download: true,
      reset: true
    }, crudMethod: { ...crudInventory }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'inventory:add'],
        edit: ['admin', 'inventory:edit'],
        del: ['admin', 'inventory:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'batch', display_name: '批次' },
        { key: 'workNum', display_name: '工程号' },
        { key: 'inventoryStatus', display_name: '库存状态' },
        { key: 'storageCode', display_name: '储位' },
        { key: 'palletCode', display_name: '载盘' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    submitCU() {
      crudInventory.edit(this.form).then(res => {
        if (res.meta.code === 200) {
          this.crud.notify(res.meta.msg, CRUD.NOTIFICATION_TYPE.SUCCESS)
          this.crud.status.edit = CRUD.STATUS.NORMAL
          this.crud.resetForm()
          this.crud.toQuery()
        } else {
          this.crud.notify(res.meta.msg, CRUD.NOTIFICATION_TYPE.ERROR)
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
